

package com.jiaai.cloud.base.util;


import com.jiaai.cloud.base.exception.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestResultGenerator {
    private static final Logger LOGGER = LoggerFactory.getLogger(RestResultGenerator.class);

    public RestResultGenerator() {
    }

    public static <T> RestResult<T> genResult(boolean success, int code, T data, String message, String description) {
        RestResult<T> result = RestResult.newInstance();
        result.setResult(success);
        result.setCode(code);
        result.setData(data);
        result.setDescription(description);
        result.setMessage(message);
        if(LOGGER.isDebugEnabled()) {
            LOGGER.debug("generate rest result:{}", result);
        }

        return result;
    }

    public static <T> RestResult<T> genSuccessResult(T data) {
        return genResult(true, 200, data, (String)null, (String)null);
    }

    public static <T> RestResult<T> genSuccessResult(String message) {
        return genResult(true, 200, (T)null, message, (String)null);
    }

    public static <T> RestResult<T> genErrorResult(String message) {
        return genResult(false, 500, (T)null, message, (String)null);
    }

    public static <T> RestResult<T> genErrorResult(int code, String message) {
        return genResult(false, code, (T)null, message, (String)null);
    }

    public static <T> RestResult<T> genErrorResult(String message, String description) {
        return genResult(false, 500, (T)null, message, description);
    }

    public static <T> RestResult<T> genErrorResult(int code, String message, String description) {
        return genResult(false, code, (T) null, message, description);
    }

    public static <T> RestResult<T> genErrorResult(ErrorCode error) {
        return genErrorResult(error.getCode().intValue(), error.getMessage());
    }

    public static RestResult genSuccessResult() {
        return genSuccessResult("操作成功");
    }
}
